<?php

class Type extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('staff_type_model' , 'STM');		
	}
	
	function index()
	{
		$this->load->view('head');
		$this->load->view('type/main');
		$this->load->view('footer');
	}
	
	function staff_type()
	{
		//get all types to display them
		$result = $this->STM->get_type();
		
		//if $result['count'] = 0 then there is no data
		//else generate array for all types
		if($result['count'] == 0){
			$data['count'] = 0;
		}
		else{
			$data['count'] = $result['count'];
			$data['result'] = $result['info'];
		}
		
		
		$this->load->view('head');
		$this->load->view('setting/type_main' , $data);
		$this->load->view('footer');		
	}
	
	function new_type($id='')
	{
		if($id!=''){
			$data['typeid']	=	$id;
			$result = $this->STM->get_type($id);
			
			//$data['typeid']		= $result['id'];
			$data['type_code']	= $result['type_code'];
			$data['description']= $result['description'];
			$data['info'] 		=  $result['info'];
			
			$data['action']		=	site_url().'/type/new_type2/'.$id;
		}
		else
			$data['action']	=	site_url().'/type/new_type2';
		$this->load->view('head');
		$this->load->view('setting/type_form' , $data);
		$this->load->view('footer');
	}
	

	
	function new_type2($id='')
	{

		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		if ($this->form_validation->run('staff_new_type') == FALSE)
		{			
			if($id!='')
				$data['action']	=	site_url().'/type/new_type2/'.$id;
			else
				$data['action']	=	site_url().'/type/new_type2';

			$this->load->view('head');
			$this->load->view('setting/type_form' , $data);
			$this->load->view('footer');
		}
		else
		{
			$data = array(
				'id' 			=> $this->input->post('typeid'),
				'type_code' 	=> $this->input->post('type_code'),
				'description' 	=> $this->input->post('description'),
				'info' 			=> $this->input->post('info')
			);
			
			//Edit:
			if(isset($id) && $id!=''){
				if($this->STM->update_type_by_id($data, $id))
				redirect('type/staff_type' , 'refresh');
			}
			else{
				if($this->STM->insert_new_type($data))
					redirect('type/staff_type' , 'refresh');
			}
		}
	}
	
	function type_delete($id)
	{
		
		if(isset($id) && $id!=''){
			//check if there is a user connect to this type before the delete
			if($this->STM->delete_type_by_id($id)){
				redirect('type/staff_type' , 'refresh');
			}
			else
			{
				echo "<script>alert('".lang('msg_delete_type_error')."');</script>";
				$this->staff_type();
			}
		}

	}

}

/* End of file type.php */
/* Location: ./system/application/controllers/type.php */
